package com.beiding.codecreater.render.java.common;

import com.beiding.codecreater.core.render.Template;
import com.beiding.codecreater.render.java.utils.NameUtils;

import java.util.HashSet;
import java.util.Set;

public abstract class MyClass {

    private MyPackage thePackage;
    private String className;
    private String description;

    //渲染模板
    private Template template;

    public MyClass(MyPackage thePackage, String className, Template template) {
        this.thePackage = thePackage;
        setClassName(className);
        thePackage.addMyClass(this);
        this.template = template;
    }

    protected Set<String> imports = new HashSet<>();

    public Set<String> getImports() {
        return imports;
    }

    public void setImports(Set<String> imports) {
        this.imports = imports;
    }

    public void addImport(String imp) {
        NameUtils.checkPackageName(imp);
        imports.add(imp);
    }

    public void setPackage(MyPackage thePackage) {
        if (thePackage == null) {
            throw new RuntimeException("类必须依附于某个包存在");
        }
        if (thePackage.equals(this.thePackage)) {
            return;
        }
        this.thePackage.remove(this);
        this.thePackage = thePackage;
        this.thePackage.addMyClass(this);
    }

    public MyPackage getPackage() {
        return thePackage;
    }

    public String getPackageName() {
        return thePackage.getName();
    }

    public String getRealClassName() {
        return getPackageName() + "." + className;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        NameUtils.checkCommonName(className);
        this.className = className;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String render() {
        return template.render(this);
    }

}
